package com.bleeth.flow.web.util;

import cn.hutool.json.JSONObject;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.util.concurrent.TimeUnit;

/**
 * @author ：Bleeth
 * @date ：2022-05-16
 * @description：缓存工具
 */
public class CacheUtil {

    private static Cache<String, JSONObject> cache = Caffeine.newBuilder()
            //cache的初始容量
            .initialCapacity(50)
            //cache最大缓存数
            .maximumSize(100)
            //设置写缓存后n秒钟过期
            .expireAfterWrite(60 * 1, TimeUnit.SECONDS)
            .build();

    public static void putCache(String key, JSONObject obj) {
        cache.put(key, obj);
    }

    public static JSONObject getCache(String key) {
        JSONObject object = cache.getIfPresent(key);
        return object;
    }
}
